#!/usr/bin/env python
# encoding: utf-8

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
import warnings
warnings.filterwarnings('ignore')

house_sale = '../data/train.csv'
data = pd.read_csv(house_sale,index_col='Id')
columns = data.columns
dict={}

#查看数据集缺失值情况
def checknull(data,columns):
    dict ={}
    for i in range(0,len(columns)):
        dict[columns[i]] = round((1460-float(data[columns[i]].describe()['count']))/1460*100,2)
    dict = sorted(dict.iteritems(), key=lambda d: d[1], reverse=False)
    for key in dict:
        print key[0],':',key[1]

#checknull(data,columns)

print data['SalePrice'].describe()

#绘制房价的直方图
sns.distplot(data['SalePrice'])
print("Skewness: %f" % data['SalePrice'].skew())
print("Kurtosis: %f" % data['SalePrice'].kurt())
plt.show()

#“房价”的相关变量分析
#‘Grlivarea’ 与 ‘SalePrice’ 散点图
var = 'GrLivArea'
GrLivArea = pd.concat([data['SalePrice'], data[var]], axis=1)
GrLivArea.plot.scatter(x=var, y='SalePrice', ylim=(0,800000))
plt.show()

var = 'TotalBsmtSF'
TotalBsmtSF = pd.concat([data['SalePrice'], data[var]], axis=1)
TotalBsmtSF.plot.scatter(x=var, y='SalePrice', ylim=(0,800000));
plt.show()

#与类别型变量的关系
#1.‘OverallQual’与‘SalePrice’箱型图
var = 'OverallQual'
OverallQual = pd.concat([data['SalePrice'], data[var]], axis=1)
f, ax = plt.subplots(figsize=(8, 6))
fig = sns.boxplot(x=var, y="SalePrice", data=OverallQual)
fig.axis(ymin=0, ymax=800000);
plt.show()

#2.‘YearBuilt’与‘SalePrice’箱型图
var = 'YearBuilt'
YearBuilt = pd.concat([data['SalePrice'], data[var]], axis=1)
f, ax = plt.subplots(figsize=(16, 8))
fig = sns.boxplot(x=var, y="SalePrice", data=YearBuilt)
fig.axis(ymin=0, ymax=800000);plt.xticks(rotation=90);
plt.show()

#客观分析
#1.相关系数矩阵
corrmat = data.corr()
print corrmat
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corrmat, vmax=.8, square=True);
plt.show()

#2.’SalePrice’ 相关系数矩阵
k = 10 #number ofvariables for heatmap
cols = corrmat.nlargest(k, 'SalePrice')['SalePrice'].index
cm = np.corrcoef(data[cols].values.T)
sns.set(font_scale=1.25)
hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10},
yticklabels=cols.values, xticklabels=cols.values)
plt.show()

#3.’SalePrice’ 和相关变量之间的散点图
sns.set()
cols = ['SalePrice', 'OverallQual', 'GrLivArea','GarageCars', 'TotalBsmtSF', 'FullBath', 'YearBuilt']
sns.pairplot(data[cols], size = 2.5)
plt.show();

